<template>
    <view class="container">
        <!-- 标题与 Logo -->
        <view class="logo-section">
            <image src="../../static/logo.png" mode="aspectFit" class="logo"></image>
            <text class="title">AInotes</text>
        </view>

        <!-- 表单部分 -->
        <view class="form-section">
            <input
                type="text"
                v-model="username"
                class="input"
                placeholder="请输入用户名"
                placeholder-class="placeholder"
            />
            <input
                type="password"
                v-model="password"
                class="input"
                placeholder="请输入密码"
                placeholder-class="placeholder"
            />
            <input
                type="password"
                v-model="passwords"
                class="input"
                placeholder="请再次输入密码"
                placeholder-class="placeholder"
            />

            <!-- 注册按钮 -->
            <button @click="zhuce" class="button">立即注册</button>

            <!-- 跳转到登录 -->
            <navigator url="../login/login" open-type="navigateBack" hover-class="none" class="link">
                已有账号？点击登录
            </navigator>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            username: '',
            password: '',
            passwords: ''
        };
    },
    methods: {
        // 注册逻辑
        zhuce() {
            if (this.username.trim() === '') {
                this.showMessage('请输入用户名');
            } else if (this.password.trim() === '') {
                this.showMessage('请输入密码');
            } else if (this.password !== this.passwords) {
                this.showMessage('两次密码不一致');
            } else {
                let userList = uni.getStorageSync('userList') || [];
                if (userList.some((user) => user.username === this.username)) {
                    this.showMessage('用户名已注册');
                } else {
                    userList.push({ username: this.username, password: this.password });
                    uni.setStorageSync('userList', userList);
                    this.showMessage('注册成功');
                    this.resetForm();
                    setTimeout(() => {
                        uni.navigateBack();
                    }, 1000);
                }
            }
        },
        // 显示提示消息
        showMessage(message) {
            uni.showToast({
                title: message,
                icon: 'none'
            });
        },
        // 重置表单
        resetForm() {
            this.username = '';
            this.password = '';
            this.passwords = '';
        }
    }
};
</script>

<style lang="scss" scoped>
/* 页面容器 */
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background-color: #f7f8fa;
    padding: 40rpx;
}

/* Logo 和标题 */
.logo-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 60rpx;

    .logo {
        width: 180rpx;
        height: 180rpx;
        margin-bottom: 20rpx;
    }

    .title {
        font-size: 48rpx;
        font-weight: bold;
        color: #7e57c2;
    }
}

/* 表单部分 */
.form-section {
    width: 100%;

    .input {
        width: 100%;
        padding: 20rpx;
        font-size: 32rpx;
        margin-bottom: 40rpx;
        border-radius: 16rpx;
        background-color: #fff;
        box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
        color: #333;
    }

    .placeholder {
        color: #bbb;
    }

    .button {
        width: 100%;
        padding: 20rpx 0;
        font-size: 36rpx;
        color: #fff;
        background-color: #7e57c2;
        border-radius: 16rpx;
        text-align: center;
        box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.2);
        transition: all 0.3s ease;
    }

    .button:active {
        background-color: #6a44a1;
    }

    .link {
        display: block;
        text-align: center;
        margin-top: 20rpx;
        font-size: 28rpx;
        color: #7e57c2;
    }
}
</style>
